package com.twu.biblioteca;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: luisarmando
 * Date: 1/25/13
 * Time: 5:20 PM
 * To change this template use File | Settings | File Templates.
 */
public class BibliotecaView {
    private Scanner scanner;
    private PrintWriter out;
    private BibliotecaController controller;

    public BibliotecaView(Scanner scanner, PrintWriter out){
        this.scanner = scanner;
        this.out = out;
        controller = new BibliotecaController(scanner, out);
    }

    public void showWelcomeMessage(){
        this.out.println("Welcome to Biblioteca!");
    }

    public void showMenuList(){
        List<String> menuList = controller.getMenus();
        this.out.println("Please select an option from the menu:");
        this.out.println("0. Exit application");
        for (int i=0; i<menuList.size(); i++){
            this.out.println(Integer.toString(i+1)+". "+menuList.get(i));
        }
    }

    public void run(){
        this.showWelcomeMessage();

        int menuOpt;
        do {
            showMenuList();
            if (!scanner.hasNext())
                break;
            menuOpt = scanner.nextInt();
            if (menuOpt != 0){
                controller.handleMenuSelection(menuOpt);
            }
        } while (menuOpt != 0);
    }

    public BibliotecaController getController() {
        return controller;
    }

    public void setController(BibliotecaController controller) {
        this.controller = controller;
    }
}
